package main

import (
	"../fileutil"
	"../picutil"
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
)

func main() {

	scanner := bufio.NewReader(os.Stdin)
	fmt.Println("请输入图片目录")
	lineBytes, _, _ := scanner.ReadLine()
	picturePath := string(lineBytes)

	ClassifyPicture(picturePath)

}

func ClassifyPicture(picturePath string) {
	// 创建输出目录
	pic2160OutPath, _ := fileutil.MkDir(picturePath, "2160")
	pic3840OutPath, _ := fileutil.MkDir(picturePath, "3840")
	pic0000OutPath, _ := fileutil.MkDir(picturePath, "0000")

	pic2160List := make([]string, 0)
	pic3840List := make([]string, 0)
	pic0000List := make([]string, 0)

	success := 0
	fail := 0
	fmt.Printf("开始处理。。。")

	// 处理图片
	pictures, _ := ioutil.ReadDir(picturePath)
	for _, picture := range pictures {
		fileName := picture.Name()
		fileutil.DealDesktop(picturePath, fileName)
		fileFullPath := filepath.Join(picturePath, fileName)
		if !fileutil.IsFile(fileFullPath) {
			continue
		}
		config := picutil.Config(fileFullPath)
		h := config.Height
		w := config.Width
		if (h == 2160 && w >= 3840) || (w == 3840 && h >= 2160) {
			pic0000List = append(pic0000List, fileName)
		} else if float32(h)/float32(w) >= 0.5625 {
			pic3840List = append(pic3840List, fileName)
		} else {
			pic2160List = append(pic2160List, fileName)
		}
	}

	// 移动图片
	success0000, fail0000 := fileutil.MoveFiles(pic0000List, picturePath, pic0000OutPath)
	fmt.Println()

	success3840, fail3840 := fileutil.MoveFiles(pic3840List, picturePath, pic3840OutPath)
	fmt.Println()

	success2160, fail2160 := fileutil.MoveFiles(pic2160List, picturePath, pic2160OutPath)
	fmt.Println()

	success = success0000 + success2160 + success3840
	fail = fail0000 + fail2160 + fail3840

	fmt.Println()
	fmt.Println("---------------------------")
	fmt.Printf("成功： %d， 失败： %d\n", success, fail)
	fmt.Println("END!")
}
